package com.apps.ftapp.activity;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MyOrderAdapter;
import com.apps.ftapp.model.OrderModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import es.dmoral.toasty.Toasty;

public class MyOrderActivity extends BaseActivity {

    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private List<OrderModel.DataBean> dataList1 = new ArrayList();
    private MyOrderAdapter myOrderAdapter;

    private Gson gson = new Gson();


    //用于记录当前是第几页
    private int curPage = 1;
    //用于记录一页应该请求多少条数据
    private int pageSize = 10;
    //用于记录总页数，在上拉的时候判断还有没有更多数据
    private boolean isEnd = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);

        setTitle("我的订单");

        initView();

        loadData();

    }

    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        params.put("page", curPage);
        Async.get("my/orders", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));

                    OrderModel result = gson.fromJson(new String(responseBody), OrderModel.class);
                    if (1 == Tools.isIntNull(result.getSuccess())) {
                        dataList1.addAll(result.getData());
                        L.i("main", "...." + result.getData().size());
                        myOrderAdapter.addRes(result.getData());

                    }

                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });

    }

    private void initView() {


        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000);
                //一般加载数据都是在子线程中，这里我用到了handler
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        curPage = 1;
//                        myOrderAdapter.clearLists()clearLists;
                        loadData();
                    }
                }, 1000);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        isLoadMore = false;
                        curPage = curPage + 1;
                        if (!isEnd) {
                            loadData();
                        } else {
                            Toasty.normal(getApplicationContext(), "没有更多啦O(∩_∩)O").show();

                        }
                    }
                }, 1000);
            }
        });



        myOrderAdapter = new MyOrderAdapter(getApplicationContext(), new ArrayList<OrderModel.DataBean>());
        listView.setAdapter(myOrderAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("id", dataList1.get(i).getId());
                ActivitySkipUtil.skipAnotherActivity(MyOrderActivity.this, MyOrderDetailActivity.class, map);
            }
        });
    }
}
